import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/route/route.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';

class MineEvaluate extends StatefulWidget {
  const MineEvaluate({Key? key}) : super(key: key);

  @override
  State<MineEvaluate> createState() => _MineEvaluateState();
}

class _MineEvaluateState extends State<MineEvaluate> {
  late ScrollController scrollController;

  bool isLoading = false;

  bool noMore = false;

  @override
  void initState() {
    super.initState();
    scrollController = ScrollController()
      ..addListener(() {
        if (!isLoading &&
            !noMore &&
            scrollController.position.pixels >= scrollController.position.maxScrollExtent) {
          setState(() {
            isLoading = true;
            loadMoreData();
          });
        }
      });
  }

  @override
  void dispose() {
    super.dispose();
    scrollController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      controller: scrollController,
      child: Column(
        children: [
          Container(
            margin: const EdgeInsets.only(top: Sizes.default20),
            decoration: const BoxDecoration(color: Colors.white),
            padding:
                const EdgeInsets.symmetric(vertical: Sizes.default20, horizontal: Sizes.default15),
            child: ListBody(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '订单号：13598979844872',
                      style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                    ),
                    TextButton(
                        onPressed: () {
                          Get.toNamed(RouteGet.evaluateMineInfo);
                        },
                        child: Text.rich(TextSpan(
                            text: '详情',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color797, fontSize: Sizes.default12),
                            children: [
                              WidgetSpan(
                                  child: BrunoTools.getAssetSizeImage(
                                      BrnAsset.iconRightArrow, Sizes.default14, Sizes.default14),
                                  alignment: PlaceholderAlignment.middle)
                            ])))
                  ],
                ),
                Container(
                  margin: const EdgeInsets.symmetric(vertical: Sizes.default20),
                  alignment: Alignment.centerLeft,
                  child: Row(
                    children: [
                      Text(
                        '配送评分：',
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color797, fontSize: Sizes.default13),
                      ),
                      BrnRatingStar(
                          count: 5,
                          selectedCount: 3.5,
                          space: 3,
                          canRatingZero: true,
                          starBuilder: (RatingState state) {
                            return buildRating(state);
                          }),
                    ],
                  ),
                ),
                Container(
                  alignment: Alignment.centerLeft,
                  child: Text(
                    '配送员：张晓楠',
                    style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                  ),
                ),
                Container(
                  margin: const EdgeInsets.symmetric(vertical: Sizes.default20),
                  alignment: Alignment.centerLeft,
                  child: Row(
                    children: [
                      Text(
                        '评价内容：服务好，配送快，速度一流！',
                        style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                      ),
                      Container(
                        constraints: const BoxConstraints.expand(
                            width: Sizes.default40, height: Sizes.default20),
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            border: Border.all(color: ColorsUtils.rgba(204, 0, 0, 1)),
                            color: ColorsUtils.rgba(255, 255, 255, 1),
                            borderRadius: BorderRadius.circular(Sizes.default2)),
                        child: Text(
                          '审核中',
                          style: StylesUtils.customTextStyle(
                              fontSize: Sizes.default12, color: ColorsUtils.colorCc0),
                        ),
                      )
                    ],
                  ),
                ),
                Container(
                  alignment: Alignment.centerLeft,
                  child: Text(
                    '评价时间：2023-05-18 12:10:17',
                    style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                  ),
                ),
              ],
            ),
          ),
          ...addItem(),
          renderBottom(),
        ],
      ),
    );
  }

  List<Widget> addItem() {
    List<Widget> adds = [];
    for (var i = 0; i < 12; i++) {
      adds.add(Container(
        margin: const EdgeInsets.only(top: Sizes.default20),
        decoration: const BoxDecoration(color: Colors.white),
        padding: const EdgeInsets.symmetric(vertical: Sizes.default20, horizontal: Sizes.default15),
        child: ListBody(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  '订单号：13598979844872',
                  style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                ),
                TextButton(
                    onPressed: () {
                      Get.toNamed(RouteGet.evaluateMineInfo);
                    },
                    child: Text.rich(TextSpan(
                        text: '详情',
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color797, fontSize: Sizes.default12),
                        children: [
                          WidgetSpan(
                              child: BrunoTools.getAssetSizeImage(
                                  BrnAsset.iconRightArrow, Sizes.default14, Sizes.default14),
                              alignment: PlaceholderAlignment.middle)
                        ])))
              ],
            ),
            Container(
              margin: const EdgeInsets.symmetric(vertical: Sizes.default20),
              alignment: Alignment.centerLeft,
              child: Row(
                children: [
                  Text(
                    '配送评分：',
                    style: StylesUtils.customTextStyle(
                        color: ColorsUtils.color797, fontSize: Sizes.default13),
                  ),
                  BrnRatingStar(
                      count: 5,
                      selectedCount: 3.5,
                      space: 3,
                      canRatingZero: true,
                      starBuilder: (RatingState state) {
                        return buildRating(state);
                      }),
                ],
              ),
            ),
            Container(
              alignment: Alignment.centerLeft,
              child: Text(
                '配送员：张晓楠',
                style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
              ),
            ),
            Container(
              margin: const EdgeInsets.symmetric(vertical: Sizes.default20),
              alignment: Alignment.centerLeft,
              child: Row(
                children: [
                  Text(
                    '评价内容：服务好，配送快，速度一流！',
                    style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                  )
                ],
              ),
            ),
            Container(
              alignment: Alignment.centerLeft,
              child: Text(
                '评价时间：2023-05-18 12:10:17',
                style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
              ),
            ),
          ],
        ),
      ));
    }
    return adds;
  }

  Widget buildRating(RatingState state) {
    switch (state) {
      case RatingState.select:
        return BrunoTools.getAssetSizeImage(BrnAsset.iconStar, Sizes.default14, Sizes.default14,
            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorFf6));
      case RatingState.half:
        return BrunoTools.getAssetSizeImage(BrnAsset.iconStarHalf, Sizes.default14, Sizes.default14,
            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorFf6));
      case RatingState.unselect:
      default:
        return BrunoTools.getAssetSizeImage(BrnAsset.iconStar, Sizes.default14, Sizes.default14,
            color: ColorsUtils.parseColorFromHexString('#A6A6A6'));
    }
  }

  Widget renderBottom() {
    if (noMore) {
      return Container(
        padding: const EdgeInsets.symmetric(vertical: 15),
        alignment: Alignment.center,
        child: Text(
          '- 已显示全部数据 -',
          style:
              StylesUtils.customTextStyle(color: ColorsUtils.color797, fontSize: Sizes.default15),
        ),
      );
    }
    if (isLoading) {
      return Container(
        padding: const EdgeInsets.symmetric(vertical: 15),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              '努力加载中...',
              style: StylesUtils.customTextStyle(
                  color: ColorsUtils.color797, fontSize: Sizes.default15),
            ),
            const Padding(padding: EdgeInsets.only(left: 10)),
            const SizedBox(
              width: 20,
              height: 20,
              child: CircularProgressIndicator(strokeWidth: 3),
            ),
          ],
        ),
      );
    } else {
      return Container(
        padding: const EdgeInsets.symmetric(vertical: 15),
        alignment: Alignment.center,
        child: Text(
          '上拉加载更多',
          style:
              StylesUtils.customTextStyle(color: ColorsUtils.color797, fontSize: Sizes.default15),
        ),
      );
    }
  }

  void loadMoreData() {
    Future.delayed(const Duration(seconds: 1), () {
      setState(() {
        noMore = true;
        isLoading = false;
      });
    });
  }
}
